<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/date.js"></script>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        li {
            list-style: none;
        }

        a {
            color: inherit;
            text-decoration: none;
        }

        .commentBar {
            width: 600px;
            margin: 0 auto;
        }

        .commentBar .commentSendBox {
            margin-top: 50px;
        }

        .commentBar .commentSendBox textarea {
            width: 100%;
            height: 200px;
            resize: none;
            padding: 10px;
            box-sizing: border-box;
            outline: none;
        }

        .commentBar .commentSendBox .sendBox {
            display: flex;
            justify-content: space-between;
        }

        .commentListBox {
            margin-top: 50px;
            font-size: 12px;
            line-height: 18px;
        }

        .showComment li {
            padding: 10px;
        }

        .showComment li+li {
            border-top: 1px dashed #333;
        }

        .comment {
            text-indent: 10px;
        }

        .commentDetail {
            display: flex;
            text-indent: 10px;
        }

        .reply {
            margin-left: 50px;
        }

        .replyList {
            padding-left: 50px;
        }

        .replyList .comment-header {
            margin-top: 12px;
        }

        .comment-bottom {
            height: 80px;
            display: flex;
            margin-top: 20px;
            display: none;
        }

        .comment-bottom .replyCon {
            flex: 1;
            height: 80px;
            border-radius: 10px;
            margin-right: 10px;
            outline: none;
            padding: 10px;
            box-sizing: border-box;
            resize: none;
        }

        .comment-bottom .replyBtn {
            width: 80px;
            height: 80px;
            background-color: #00b5e5;
            border-radius: 10px;
            word-break: break-all;
            font-size: 24px;
            word-spacing: 10px;
            line-height: 40px;
            text-align: center;
        }
    </style>
</head>

<body>
    <div class="commentBar">
        <div class="commentSendBox">
            <textarea name="" id="" cols="30" rows="10" class="commentCon"></textarea>
            <div class="sendBox">
                <div class="commentTips">您还可以输入50个字</div>
                <button class="commentSend">发送</button>
            </div>
        </div>
        <div class="commentListBox">
            <ul class="showComment">
                <!-- <li>
                    点击发送是的评论内容
                    <div class="comment-header">
                        <div class="nick">PigZhang</div>
                        <div class="comment">天对地，雨对风，黑心棉对白岩松</div>
                        <div class="commentDetail">
                            <div class="time">2021-05-16 18:03</div>
                            <div class="reply">回复</div>
                        </div>
                    </div>
                    用于存放 留言回复
                    <div class="replyList">
                        <div class="comment-header">
                            <div class="nick">PigZhang: <span class="comment">天对地，雨对风，黑心棉对白岩松</span></div>
                            <div class="commentDetail">
                                <div class="time">2021-05-16 18:03</div>
                            </div>
                        </div>
                        <div class="comment-header">
                            <div class="nick">PigZhang: <span class="comment">天对地，雨对风，黑心棉对白岩松</span></div>
                            <div class="commentDetail">
                                <div class="time">2021-05-16 18:03</div>
                            </div>
                        </div>
                        <div class="comment-header">
                            <div class="nick">PigZhang: <span class="comment">天对地，雨对风，黑心棉对白岩松</span></div>
                            <div class="commentDetail">
                                <div class="time">2021-05-16 18:03</div>
                            </div>
                        </div>
                    </div>
                    用于回复的文本框
                    <div class="comment-bottom">
                        <textarea class="replyCon" name="" id="" cols="30" rows="10"></textarea>
                        <a class="replyBtn" href="javascript:;">发表 评论</a>
                    </div>
                </li> -->
            </ul>
        </div>
    </div>
</body>
<script>
    // 1. 评论框输入提示
    // a.  onkeypress 按下触发 此时内容还没有输入到文本框中 => 错位了一个 (在绑定一个onkeyup)
    // b.  评论框虽然做了计数 但是该超还会超 => (1) return false(阻止默认输入)
    // c.  中文输入法,悬浮在输入框之上, 此时内容依旧可以输入到框中  =>  只截取前50个字符,剩余的不要

    // 2. 点击发送 快捷键发送(ctrl+enter)
    // 3. 留言回复

    var commentCon = document.getElementsByClassName("commentCon")[0];
    var commentTips = document.getElementsByClassName("commentTips")[0];
    var commentSend = document.getElementsByClassName("commentSend")[0];
    var showComment = document.getElementsByClassName("showComment")[0];

    // HTMLCollection   => 可以随页面同 类型/类名元素的增加或减少 自动更新集合
    var replyList = document.getElementsByClassName("reply");
    var replySubList = document.getElementsByClassName("replyList");
    var commentBottomList = document.getElementsByClassName("comment-bottom");
    var replyConList = document.getElementsByClassName("replyCon");
    var replyBtnList = document.getElementsByClassName("replyBtn");


    commentCon.onkeypress = function(e){
        var e = e ||window.event;
        var keyCode = e.which || e.keyCode;

        //keypress 提供了ctrl+enter 的组合键  =>  keyCode == 10
        if(keyCode == 10){ // ctrl + enter
            commentSendhandler();
        }
    }


    commentSend.onclick = commentSendhandler;
    
    
    function commentSendhandler(){
        var comment = commentCon.value.trim();
        if(comment){

            var li = document.createElement("li");
            li.innerHTML = ` <div class="comment-header">
                        <div class="nick">PigZhang</div>
                        <div class="comment">${comment}</div>
                        <div class="commentDetail">
                            <div class="time">${dateFormat("YY-MM-DD hh:mm")}</div>
                            <div class="reply">回复</div>
                        </div>
                    </div>
                    <div class="replyList">
                       
                    </div>
                    <div class="comment-bottom">
                        <textarea class="replyCon" name="" id="" cols="30" rows="10"></textarea>
                        <a class="replyBtn" href="javascript:;">发表 评论</a>
                    </div>`;

            showComment.prepend(li);
            commentCon.value = "";
        }
    }

    // 事件委托  => 可以给未来生成元素绑定事件
    showComment.onclick = function(e){
        var e = e ||window.event;
        var target = e.target || e.srcElement;
        if(target.className == "reply"){
            console.log("点击了回复");

            var index = Array.from(replyList).indexOf(target);
            console.log(index);
            
            for(var j=0;j<commentBottomList.length;j++){
                commentBottomList[j].style.display = "none";
            }
            commentBottomList[index].style.display = "flex";
        }else if(target.className == "replyBtn"){
            var index = Array.from(replyBtnList).indexOf(target);
            console.log(index);
            var subComment = replyConList[index].value.trim();

            var div = document.createElement("div");
            div.className = "comment-header";
            div.innerHTML = `
                            <div class="nick">PigZhang: <span class="comment">${subComment}</span></div>
                            <div class="commentDetail">
                                <div class="time">${dateFormat("YY-MM-DD hh:mm")}</div>
                            </div>
                        `

            // 找到对应下标的 子回复列表  回复框   回复框的盒子
            replySubList[index].prepend(div);
            replyConList[index].value = "";
            commentBottomList[index].style.display = "none";
            
        }
    }







</script>

</html>